//
//  LSBannerSubView.m
//  ChartDemo
//
//  Created by 盛超 on 2018/10/19.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "LSBannerSubView.h"
#import "UIColor+RGB.h"
#import "LSResFirstController_version2.h"
#import "KZUnfamiliarVocabularyImproveVC_version2.h"
#import "KZListenSpeakVocabularyImporveVC.h"
#import "KZPronounceImproveVC.h"
#import "KZUserMyNewWordVC.h"
#import "LSWrongBookController.h"

@interface LSBannerSubView ()<CAAnimationDelegate>
/**
 曲线图背景
 */
@property (nonatomic,strong) UIView *lineChartView;
/**
 按钮
 */
@property (nonatomic,strong) KZShadowButton *enterBtn;
/**
 线的坐标点数组
 */
@property (nonatomic,strong) NSMutableArray *line_points;
/**
 各能力数值的坐标点
 */
@property (nonnull,strong) NSMutableArray *value_points;
/**
 渐变曲线
 */
@property (nonatomic,strong) CAShapeLayer *maskLayer;
/**
 渐变曲线阴影
 */
@property (nonatomic,strong) CAShapeLayer *shadowLayer;
/**
 白色遮罩
 */
@property (nonatomic,strong) CAGradientLayer *gradientLayerWhite;
/**
 印章
 */
@property (nonatomic,strong) UIImageView *markImageView;
/**
 最后的能力点
 */
@property (nonatomic,strong) UIView *lastCircle;
/**
 分数背景
 */
@property (nonatomic,strong) UIImageView *tipImageView;
/**
 分数值
 */
@property (nonatomic,strong) UILabel *tipLabel;

/*
 能力等级
 */
@property (nonatomic,assign) NSInteger level;//1-差，2-良，3-优

@property (nonatomic,strong) UIColor *shdowColor;
@property (nonatomic,strong) NSArray *colors1;
@property (nonatomic,strong) NSArray *colors2;

@end

@implementation LSBannerSubView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
       // [self initialize];
    }
    
    return self;
}

- (instancetype)init {
    if (self = [super init]) {
       // [self initialize];
    }
    return self;
}


- (void)setWeak:(LSweak *)weak {
    _weak = weak;
    
    if (!_enterBtn) {
        _enterBtn = [[KZShadowButton alloc] init:^(UIButton *button) {
            [button setTitle:@"进入训练" forState:UIControlStateNormal];
        }];
        _enterBtn.frame = CGRectMake((self.frame.size.width-self.frame.size.width*260.0/338)/2,200+self.frame.size.height-(30+40)/268.0*self.frame.size.height,self.frame.size.width*260.0/338,self.frame.size.height*40.0/268);
        [self addSubview:_enterBtn];
        [_enterBtn addTarget:self action:@selector(enterAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self getWeakLevel];
    
    [self getDateArr];
    
    [self getValueArr];
    
    [self initialize];
}

#pragma mark - 分析颜色和优良差
- (void)getWeakLevel {
    
    if ([_weak.AbiName isEqualToString:@"听力能力"]) {
        
        if ([_weak.AbiResult isEqualToString:@"4"]) {
            _level = 1;//差
            //黄色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0xfe4e46].CGColor,
                        (__bridge id)[UIColor colorWithHex:0xf5b600].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0xf5ab00].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0xf5ab00] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0xf5ab00] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor orangeColor];
            
            [_enterBtn.button setTitle:@"听力能力强化训练" forState:UIControlStateNormal];
            
        } else if ([_weak.AbiResult isEqualToString:@"2"] || [_weak.AbiResult isEqualToString:@"3"]) {
            _level = 2;//良
            //蓝色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0x1496e4].CGColor,
                        (__bridge id)[UIColor colorWithHex:0x2ac7f4].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0x5ab7f3].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x5ab7f3] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x5ab7f3] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor colorWithHex:0x2ac7f4];
            
            [_enterBtn.button setTitle:@"错题生词强化训练" forState:UIControlStateNormal];
            
        } else {
            
            _level = 3;//优
            //绿色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0x49c537].CGColor,
                        (__bridge id)[UIColor colorWithHex:0x14cbac].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0x8dce5b].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x8dce5b] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x8dce5b] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor colorWithHex:0x14cbac];
            
            [_enterBtn.button setTitle:@"错题本查看" forState:UIControlStateNormal];
            
        }
        
    } else if ([_weak.AbiName isEqualToString:@"口语能力"] && [_weak.AbiScoreType isEqualToString:@"发音能力"]) {
        
        if ([_weak.AbiResult isEqualToString:@"4"]) {
            _level = 1;//差
            //黄色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0xfe4e46].CGColor,
                        (__bridge id)[UIColor colorWithHex:0xf5b600].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0xf5ab00].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0xf5ab00] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0xf5ab00] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor orangeColor];
            
            [_enterBtn.button setTitle:@"标准发音强化训练" forState:UIControlStateNormal];
            
        } else if ([_weak.AbiResult isEqualToString:@"2"] || [_weak.AbiResult isEqualToString:@"3"]) {
            
            _level = 2;//良
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0x1496e4].CGColor,
                        (__bridge id)[UIColor colorWithHex:0x2ac7f4].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0x5ab7f3].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x5ab7f3] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x5ab7f3] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor colorWithHex:0x2ac7f4];
            
             [_enterBtn.button setTitle:@"生词本查看" forState:UIControlStateNormal];
        } else {
            
            _level = 3;//优
            //绿色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0x49c537].CGColor,
                        (__bridge id)[UIColor colorWithHex:0x14cbac].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0x8dce5b].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x8dce5b] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x8dce5b] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor colorWithHex:0x14cbac];
             [_enterBtn.button setTitle:@"生词本查看" forState:UIControlStateNormal];
            
        }
        
    } else if ([_weak.AbiName isEqualToString:@"口语能力"] && [_weak.AbiScoreType isEqualToString:@"朗读能力"]) {
        
        if ([_weak.AbiResult isEqualToString:@"4"]) {
            _level = 1;//差
            //黄色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0xfe4e46].CGColor,
                        (__bridge id)[UIColor colorWithHex:0xf5b600].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0xf5ab00].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0xf5ab00] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0xf5ab00] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor orangeColor];
            
            [_enterBtn.button setTitle:@"朗读能力强化训练" forState:UIControlStateNormal];
            
        } else if ([_weak.AbiResult isEqualToString:@"2"] || [_weak.AbiResult isEqualToString:@"3"]) {
            
            _level = 2;//良
            //蓝色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0x1496e4].CGColor,
                        (__bridge id)[UIColor colorWithHex:0x2ac7f4].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0x5ab7f3].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x5ab7f3] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x5ab7f3] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor colorWithHex:0x2ac7f4];
            
             [_enterBtn.button setTitle:@"错题生词强化训练" forState:UIControlStateNormal];
            
        } else {
            
            _level = 3;//优
            //绿色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0x49c537].CGColor,
                        (__bridge id)[UIColor colorWithHex:0x14cbac].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0x8dce5b].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x8dce5b] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x8dce5b] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor colorWithHex:0x14cbac];
            
            [_enterBtn.button setTitle:@"错题本查看" forState:UIControlStateNormal];
        }
        
    } else if ([_weak.AbiName isEqualToString:@"词汇量"] && [_weak.AbiScoreType isEqualToString:@"听力词汇量"]) {
        
        if ([_weak.AbiResult isEqualToString:@"4"]) {
            _level = 1;//差
            //黄色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0xfe4e46].CGColor,
                        (__bridge id)[UIColor colorWithHex:0xf5b600].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0xf5ab00].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0xf5ab00] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0xf5ab00] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor orangeColor];
            
            [_enterBtn.button setTitle:@"听说词汇强化训练" forState:UIControlStateNormal];
            
        } else if ([_weak.AbiResult isEqualToString:@"2"] || [_weak.AbiResult isEqualToString:@"3"]) {
            
            _level = 2;//良
            //蓝色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0x1496e4].CGColor,
                        (__bridge id)[UIColor colorWithHex:0x2ac7f4].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0x5ab7f3].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x5ab7f3] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x5ab7f3] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor colorWithHex:0x2ac7f4];
            
            [_enterBtn.button setTitle:@"生词本查看" forState:UIControlStateNormal];
            
        }
        else {
           
            _level = 3;//优
             //绿色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0x49c537].CGColor,
                        (__bridge id)[UIColor colorWithHex:0x14cbac].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0x8dce5b].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x8dce5b] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x8dce5b] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor colorWithHex:0x14cbac];
            
            [_enterBtn.button setTitle:@"生词本查看" forState:UIControlStateNormal];
        }
        
        
    } else if ([_weak.AbiName isEqualToString:@"词汇量"] && [_weak.AbiScoreType isEqualToString:@"朗读词汇量"]) {
        
        if ([_weak.AbiResult isEqualToString:@"4"]) {
            _level = 1;//差
            //黄色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0xfe4e46].CGColor,
                        (__bridge id)[UIColor colorWithHex:0xf5b600].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0xf5ab00].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0xf5ab00] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0xf5ab00] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor orangeColor];
            
            [_enterBtn.button setTitle:@"听说词汇强化训练" forState:UIControlStateNormal];
            
        } else if ([_weak.AbiResult isEqualToString:@"2"] || [_weak.AbiResult isEqualToString:@"3"]) {
            
            _level = 2;//良
            //蓝色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0x1496e4].CGColor,
                        (__bridge id)[UIColor colorWithHex:0x2ac7f4].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0x5ab7f3].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x5ab7f3] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x5ab7f3] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor colorWithHex:0x2ac7f4];
            
            [_enterBtn.button setTitle:@"生词本查看" forState:UIControlStateNormal];
            
        } else {
            
            _level = 3;//优
            //绿色
            _colors1 =@[(__bridge id)[UIColor colorWithHex:0x49c537].CGColor,
                        (__bridge id)[UIColor colorWithHex:0x14cbac].CGColor];
            _colors2 = @[(__bridge id)[UIColor colorWithHex:0x8dce5b].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x8dce5b] colorWithAlphaComponent:0.8].CGColor,
                         (__bridge id)[[UIColor colorWithHex:0x8dce5b] colorWithAlphaComponent:0].CGColor];
            _shdowColor = [UIColor colorWithHex:0x14cbac];
            
            [_enterBtn.button setTitle:@"生词本查看" forState:UIControlStateNormal];
            
        }
        
    }
        
}

#pragma mark - 时间、分数 数据处理
- (void)getDateArr {
    //全部能力的时间
    LSweak *weak =  _weak;
    NSArray *array = [weak.ZDTimeString componentsSeparatedByString:@","];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *endDate = [dateFormatter dateFromString:array.lastObject];
    NSTimeInterval end = [endDate timeIntervalSince1970]*1;
    //单个能力的时间
    NSMutableArray *dateArr = [NSMutableArray array];
    for (NSInteger j = 4; j>=0; j--) {
        NSTimeInterval time = end-j*24*3600;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
        NSString *timeStr = [dateFormatter stringFromDate:date];
        
        [dateArr addObject:timeStr];
    }
    [dateArr insertObject:@"&1" atIndex:0];
    [dateArr insertObject:@"&2" atIndex:dateArr.count];
   // NSLog(@"%@",dateArr);
    _dateArr = [NSArray arrayWithArray:dateArr];
}

- (void)getValueArr {
  
    LSweak *weak = _weak;
    NSArray *arrayScore = [weak.ZDScoreString componentsSeparatedByString:@","];
    NSArray *arrayTime = [weak.ZDTimeString componentsSeparatedByString:@","];
    
    NSMutableDictionary *valueDict = [NSMutableDictionary dictionary];
    for (NSInteger j = 0; j<arrayTime.count; j++) {
        [valueDict setObject:arrayScore[j] forKey:arrayTime[j]];
    }
    
    NSString *str1 = [valueDict objectForKey:_dateArr[1]];
    NSString *str2 = [valueDict objectForKey:_dateArr[_dateArr.count-2]];
    
    [valueDict setObject:NotNull(str1) forKey:@"&1"];
    [valueDict setObject:NotNull(str2) forKey:@"&2"];
    NSLog(@"%@",valueDict);
    _valueDict = [NSDictionary dictionaryWithDictionary:valueDict];
}

- (void)initialize {
    
    self.layer.backgroundColor = [UIColor whiteColor].CGColor;
    
    _value_points = [NSMutableArray array];
    _line_points = [NSMutableArray array];
    
    if (!_lineChartView) {
        _lineChartView = [[UIView alloc] initWithFrame:CGRectMake(35,0,self.frame.size.width-70,self.frame.size.height*151.0/268)];
        [self addSubview:_lineChartView];
    }
    
    for (NSInteger i = 0; i<_dateArr.count; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(_lineChartView.frame.origin.x+i*_lineChartView.frame.size.width/7, _lineChartView.frame.origin.y+_lineChartView.frame.size.height, _lineChartView.frame.size.width/7, 17)];
        label.font = [UIFont systemFontOfSize:11];
        if (iPhone5S) {
            label.font = [UIFont systemFontOfSize:10];
        }
        label.textColor = [UIColor colorWithHex:0xbbbaba];
        label.textAlignment = NSTextAlignmentCenter;
        if (![_dateArr[i] isEqualToString:@"&1"] && ![_dateArr[i] isEqualToString:@"&2"]) {
            NSString *string =[[_dateArr[i] componentsSeparatedByString:@" "].firstObject substringFromIndex:5];
            label.text = [string stringByReplacingOccurrencesOfString:@"-" withString:@"."];
        }
        
        [self addSubview:label];
        
        CGFloat Ymax = 113.0/268 * self.frame.size.height;
        NSString *value = [_valueDict objectForKey:_dateArr[i]];
        if (value) {
            CGFloat Yvaule = value.floatValue/100 * Ymax;
            CGPoint point = CGPointMake(label.center.x-_lineChartView.frame.origin.x, _lineChartView.frame.size.height-Yvaule);
            [_value_points addObject:@(point)];
        }
      
        if (i<=_dateArr.count-2) {
            CGPoint linepoint = CGPointMake(label.frame.origin.x+label.frame.size.width, label.frame.origin.y+label.frame.size.height);
            [_line_points addObject:@(linepoint)];
        }
    }
    [self bringSubviewToFront:_lineChartView];
    _lineChartView.clipsToBounds = NO;
    
    [self drawLine];
    
    UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(22,15,104,28)];
    titlelabel.text = _title;
    titlelabel.textAlignment = NSTextAlignmentLeft;
    titlelabel.font = [UIFont systemFontOfSize:25];
    if (iPhone5S) {
        titlelabel.font = [UIFont systemFontOfSize:20];
    }
    [self addSubview:titlelabel];
    
    UIImageView *markImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width-95,0,95,70)];
    self.markImageView = markImageView;
    if (_level==1) {
        markImageView.image = [UIImage imageNamed:@"LS-差"];
    } else if (_level==2) {
        markImageView.image = [UIImage imageNamed:@"LS-良"];
    } else if (_level==3) {
        markImageView.image = [UIImage imageNamed:@"LS-优"];
    }
    
    [self addSubview:markImageView];
    
    self.lastCircle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    self.lastCircle.backgroundColor = [UIColor whiteColor];
    self.lastCircle.layer.cornerRadius = 7.5;
    self.lastCircle.layer.borderColor = [UIColor colorWithHex:0xff8800].CGColor;
    self.lastCircle.layer.borderWidth = 4;
    self.lastCircle.layer.masksToBounds = YES;
    self.lastCircle.center = ((NSValue*)_value_points[_value_points.count-2]).CGPointValue;
    [self.lineChartView addSubview:self.lastCircle];
    
    self.tipImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 25)];
    self.tipImageView.image = [UIImage imageNamed:@"LS-分数"];
    CGPoint point = ((NSValue*)_value_points[_value_points.count-2]).CGPointValue;
    self.tipImageView.center = CGPointMake(point.x, point.y-self.lastCircle.frame.size.height/2-self.tipImageView.frame.size.height/2);
    [self.lineChartView addSubview:self.tipImageView];
    
    self.tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 10)];
    self.tipLabel.font = [UIFont boldSystemFontOfSize:10];
    self.tipLabel.textColor = [UIColor whiteColor];
    self.tipLabel.textAlignment = NSTextAlignmentCenter;
    NSString *time = _dateArr[_dateArr.count-2];
    self.tipLabel.text = [_valueDict objectForKey:time];
    self.tipLabel.center = CGPointMake(self.tipImageView.center.x, self.tipImageView.center.y-2.5);
    [self.lineChartView addSubview:self.tipLabel];
    
    self.tipImageView.layer.transform = CATransform3DMakeScale(0,0,0);
    self.tipLabel.layer.transform = CATransform3DMakeScale(0,0,0);
    
    for (NSInteger i = 0; i<_line_points.count; i++) {
        CGPoint linepoint = ((NSValue*)_line_points[i]).CGPointValue;
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(linepoint.x,self.lineChartView.frame.size.height,0.25,linepoint.y)];
        line.tag = 2048+i;
        line.backgroundColor = [UIColor colorWithHex:0xf3f3f3];
        line.alpha = 0.3;
        [self addSubview:line];
    }
}

- (void)drawRect:(CGRect)rect {
    
}

- (void)drawLine {
    
    //0.渐变遮罩
    //0.1 遮罩路径
   // CGFloat Y = [self getMinPointY]
    UIBezierPath *bezierPath2 = [UIBezierPath bezierPath];
    for (NSInteger i = 0; i<_value_points.count; i++) {
        CGPoint point = ((NSValue*)_value_points[i]).CGPointValue;
        if (i==0) {
            [bezierPath2 moveToPoint:CGPointMake(point.x,_lineChartView.frame.size.height)];
            [bezierPath2 addLineToPoint:point];
        } else {
            CGPoint from = ((NSValue*)(_value_points[i-1])).CGPointValue;
            CGPoint to = point;
            CGPoint midPoint = [self midPointBetweenPoint1:from andPoint2:to];
            [bezierPath2 addQuadCurveToPoint:midPoint controlPoint:[self controlPointBetweenPoint1:midPoint andPoint2:from]];
            [bezierPath2 addQuadCurveToPoint:to controlPoint:[self controlPointBetweenPoint1:midPoint andPoint2:to]];
        }
        
        if (i==_value_points.count-1) {
            [bezierPath2 addLineToPoint:CGPointMake(point.x,_lineChartView.frame.size.height)];
        }
    }
    [bezierPath2 closePath];
    
    //0.2 多层渐变色遮罩
    //遮罩1
    CAShapeLayer *maskLayer2 = [CAShapeLayer layer];
    maskLayer2.path = bezierPath2.CGPath;
    maskLayer2.fillColor = [UIColor whiteColor].CGColor;
    maskLayer2.strokeColor = [UIColor clearColor].CGColor;
    CAGradientLayer *gradientLayer2 = [CAGradientLayer layer];
    gradientLayer2.locations = @[@0,@0.5,@1];
    gradientLayer2.startPoint = CGPointMake(0,0);
    gradientLayer2.endPoint = CGPointMake(0,1);
    gradientLayer2.colors = _colors2;
//    gradientLayer2.colors = @[(__bridge id)[UIColor colorWithHex:0xf5ab00].CGColor,
//                              (__bridge id)[[UIColor colorWithHex:0xf5ab00] colorWithAlphaComponent:0.8].CGColor,
//                              (__bridge id)[[UIColor colorWithHex:0xf5ab00] colorWithAlphaComponent:0].CGColor];
    gradientLayer2.frame = _lineChartView.bounds;
    gradientLayer2.mask = maskLayer2;
    [_lineChartView.layer addSublayer:gradientLayer2];
    //遮罩3
    CAShapeLayer *maskLayer3 = [CAShapeLayer layer];
    maskLayer3.path = bezierPath2.CGPath;
    maskLayer3.fillColor = [UIColor whiteColor].CGColor;
    maskLayer3.strokeColor = [UIColor clearColor].CGColor;
    CAGradientLayer *gradientLayer3 = [CAGradientLayer layer];
    gradientLayer3.locations = @[@0,@0.8];
    gradientLayer3.startPoint = CGPointMake(0,0.5);
    gradientLayer3.endPoint = CGPointMake(1,0.5);
    gradientLayer3.colors = @[(__bridge id)[UIColor whiteColor].CGColor,(__bridge id)[[UIColor whiteColor] colorWithAlphaComponent:0].CGColor];
    gradientLayer3.frame = _lineChartView.bounds;
    gradientLayer3.mask = maskLayer3;
    [_lineChartView.layer addSublayer:gradientLayer3];
    //遮罩3
    CAShapeLayer *maskLayer4 = [CAShapeLayer layer];
    maskLayer4.path = bezierPath2.CGPath;
    maskLayer4.fillColor = [UIColor whiteColor].CGColor;
    maskLayer4.strokeColor = [UIColor clearColor].CGColor;
    CAGradientLayer *gradientLayer4 = [CAGradientLayer layer];
    gradientLayer4.locations = @[@0,@0.6];
    gradientLayer4.startPoint = CGPointMake(0,0);
    gradientLayer4.endPoint = CGPointMake(0,1);
    gradientLayer4.colors = @[(__bridge id)[UIColor whiteColor].CGColor,(__bridge id)[[UIColor whiteColor] colorWithAlphaComponent:0].CGColor];
    gradientLayer4.frame = _lineChartView.bounds;
    gradientLayer4.mask = maskLayer4;
    [_lineChartView.layer addSublayer:gradientLayer4];
    //白色遮罩
    CAShapeLayer *maskLayerWhite = [CAShapeLayer layer];
    maskLayerWhite.path = bezierPath2.CGPath;
    maskLayerWhite.fillColor = [UIColor redColor].CGColor;
    maskLayerWhite.strokeColor = [UIColor clearColor].CGColor;
    CAGradientLayer *gradientLayerWhite = [CAGradientLayer layer];
    self.gradientLayerWhite = gradientLayerWhite;
    gradientLayerWhite.locations = @[@0,@1];
    gradientLayerWhite.startPoint = CGPointMake(0,0);
    gradientLayerWhite.endPoint = CGPointMake(1,0);
    gradientLayerWhite.colors = @[(__bridge id)[[UIColor whiteColor] colorWithAlphaComponent:0].CGColor,(__bridge id)[UIColor whiteColor].CGColor];
    gradientLayerWhite.frame = _lineChartView.bounds;
    gradientLayerWhite.mask = maskLayerWhite;
    [_lineChartView.layer addSublayer:gradientLayerWhite];
    
    //1.曲线路径
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    for (NSInteger i = 0; i<_value_points.count; i++) {
        CGPoint point = ((NSValue*)_value_points[i]).CGPointValue;
        if (i==0) {
            [bezierPath moveToPoint:point];
        } else {
            CGPoint from = ((NSValue*)(_value_points[i-1])).CGPointValue;
            CGPoint to = point;
            CGPoint midPoint = [self midPointBetweenPoint1:from andPoint2:to];
            [bezierPath addQuadCurveToPoint:midPoint controlPoint:[self controlPointBetweenPoint1:midPoint andPoint2:from]];
            [bezierPath addQuadCurveToPoint:to controlPoint:[self controlPointBetweenPoint1:midPoint andPoint2:to]];
        }
    }
    
    //2.曲线阴影
    CAShapeLayer *shadowLayer = [CAShapeLayer layer];
    self.shadowLayer = shadowLayer;
    shadowLayer.lineWidth = 2;
    shadowLayer.path = bezierPath.CGPath;
    shadowLayer.fillColor = [UIColor clearColor].CGColor;
    shadowLayer.strokeColor = [UIColor whiteColor].CGColor;
    shadowLayer.shadowOpacity = 0.3;
    shadowLayer.shadowColor = _shdowColor.CGColor;//[UIColor orangeColor].CGColor;
    shadowLayer.shadowOffset= CGSizeMake(0,8);
    shadowLayer.shadowRadius= 1;
    [_lineChartView.layer addSublayer:shadowLayer];
    
    //3.渐变曲线
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    self.maskLayer = maskLayer;
    maskLayer.lineWidth = 2;
    maskLayer.path = bezierPath.CGPath;
    maskLayer.fillColor = [UIColor clearColor].CGColor;
    maskLayer.strokeColor = [UIColor whiteColor].CGColor;
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.locations = @[@0,@1];
    gradientLayer.startPoint = CGPointMake(0,0.5);
    gradientLayer.endPoint = CGPointMake(1,0.5);
//    gradientLayer.colors = @[(__bridge id)[UIColor colorWithHex:0xfe4e46].CGColor,(__bridge id)[UIColor colorWithHex:0xf5b600].CGColor];
    gradientLayer.colors = _colors1;
    gradientLayer.frame = _lineChartView.bounds;
    gradientLayer.mask = maskLayer;
    
    [_lineChartView.layer addSublayer:gradientLayer];
}

//计算绘制贝塞尔曲线controlPoint
- (CGPoint)controlPointBetweenPoint1:(CGPoint)point1 andPoint2:(CGPoint)point2 {
    CGPoint controlPoint = [self midPointBetweenPoint1:point1 andPoint2:point2];
    CGFloat diffY = abs((int) (point2.y - controlPoint.y));
    if (point1.y < point2.y)
        controlPoint.y += diffY;
    else if (point1.y > point2.y)
        controlPoint.y -= diffY;
    return controlPoint;
}

- (CGPoint)midPointBetweenPoint1:(CGPoint)point1 andPoint2:(CGPoint)point2 {
    return CGPointMake((point1.x + point2.x) / 2, (point1.y + point2.y) / 2);
}

//计算最小的能力点
- (CGFloat)getMinPointY {
    CGPoint point = ((NSValue*)_value_points[0]).CGPointValue;
    CGFloat Y = point.y;
    for (NSInteger i = 1; i<_value_points.count; i++) {
        CGPoint point1 = ((NSValue*)_value_points[i]).CGPointValue;
        if (Y < point1.y) {
            Y = point1.y;
        }
    }
    
    return Y;
}

#pragma mark - 点击事件
- (void)enterAction:(id)sender {
    ByBaseVC *fathervc = (ByBaseVC*)[self getCurrentViewController];
    NSString *text = _enterBtn.button.titleLabel.text;
    if ([text isEqualToString:@"听力能力强化训练"]) {
        
        LSResFirstController_version2 *listen = [LSResFirstController_version2 new];
        listen.TypeID = @"SE";
        listen.UpdateTime = _weak.UpdateTime;
        [fathervc push:listen];
        
    } else if ([text isEqualToString:@"错题生词强化训练"]) {
        
        KZUnfamiliarVocabularyImproveVC_version2 *vc = [[KZUnfamiliarVocabularyImproveVC_version2 alloc] init];
        [fathervc push:vc];
        
    } else if ([text isEqualToString:@"听说词汇强化训练"]) {
        
        KZListenSpeakVocabularyImporveVC *vc = [[KZListenSpeakVocabularyImporveVC alloc] init];
        [fathervc push:vc];
        
    } else if ([text isEqualToString:@"标准发音强化训练"]) {
        
        KZPronounceImproveVC *vc = [[KZPronounceImproveVC alloc] init];
        [fathervc push:vc];
        
    } else if ([text isEqualToString:@"朗读能力强化训练"]) {
        
        LSResFirstController_version2 *read = [LSResFirstController_version2 new];
        read.TypeID = @"Spn";
        read.UpdateTime = _weak.UpdateTime;
        [fathervc push:read];
    
    } else if([text isEqualToString:@"错题本查看"]) {
       
        LSWrongBookController *vc  = [LSWrongBookController new];
        [fathervc push:vc];
        
    } else if([text isEqualToString:@"生词本查看"]) {
        
        KZUserMyNewWordVC *vc = [KZUserMyNewWordVC new];
        [fathervc push:vc];
    }
    
    
}

#pragma mark - 开始动画
- (void)startAnimation {
    
    if (_isShowAnimation) {
        [self startScale];
        return;
    }
    _isShowAnimation = YES;
    //动画1 （曲线的动画）
    CABasicAnimation *strokeAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    strokeAnimation.duration = 1;
    strokeAnimation.repeatCount = 1;
    strokeAnimation.fromValue = @0;
    strokeAnimation.toValue = @1;
    //以下两行同时设置才能保持移动后的位置状态不变
    strokeAnimation.fillMode=kCAFillModeForwards;
    strokeAnimation.removedOnCompletion = NO;
    [self.maskLayer addAnimation:strokeAnimation forKey:@"strokeAnimation"];
    [self.shadowLayer addAnimation:strokeAnimation forKey:@"strokeAnimation"];
    
    //动画2（遮罩的动画）
    CABasicAnimation *fadeAnimation = [CABasicAnimation animationWithKeyPath:@"locations"];
    fadeAnimation.delegate = self;
    fadeAnimation.fromValue = @[@(0),@(0)];
    fadeAnimation.toValue = @[@(1),@(1)];
    fadeAnimation.duration = 1.1;
    fadeAnimation.repeatCount = 1;
    //以下两行同时设置才能保持移动后的位置状态不变
    fadeAnimation.fillMode=kCAFillModeForwards;
    fadeAnimation.removedOnCompletion = NO;
    [self.gradientLayerWhite addAnimation:fadeAnimation forKey:@"fadeAnimation"];
    
    //动画3（印章的动画）
    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    animGroup.duration = 0.7;
    animGroup.repeatCount = 1;
    animGroup.delegate  =self;
    animGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.duration = 0.7;
    scaleAnimation.repeatCount = 1;
    scaleAnimation.fromValue = @2.0;
    scaleAnimation.toValue = @1;
    scaleAnimation.removedOnCompletion = NO;
    scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    CABasicAnimation *posiAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
    posiAnimation.duration = 0.7;
    posiAnimation.fromValue = @(70*2/2);
    posiAnimation.toValue = @(70/2);
    posiAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    
    animGroup.animations = @[scaleAnimation,posiAnimation];
   
    
    [self.markImageView.layer addAnimation:animGroup forKey:@"animGroup"];
    
    //动画4(圆点动画)
    CABasicAnimation *scaleAnimation2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation2.delegate = self;
    scaleAnimation2.duration = 1.0;
    scaleAnimation2.repeatCount = 1;
    scaleAnimation2.fromValue = @0;
    scaleAnimation2.toValue = @1;
    scaleAnimation2.removedOnCompletion = NO;
    [self.lastCircle.layer addAnimation:scaleAnimation2 forKey:@"scaleAnimation2"];
    
    //动画5
    [self lineAnimation];
    
    //动画6
    [self enterBtnAnimation];
}

- (void)lineAnimation
{
    UILabel *line = [self viewWithTag:2048];
    CGPoint startPoint = line.layer.position;
    
    CABasicAnimation *posiAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
    posiAnimation.duration = 0.2;
    posiAnimation.fromValue = @(startPoint.y);
    posiAnimation.toValue = @(self.lineChartView.frame.size.height/2);
    posiAnimation.repeatCount = 1;
    //以下两行同时设置才能保持移动后的位置状态不变
    posiAnimation.fillMode=kCAFillModeForwards;
    posiAnimation.removedOnCompletion = NO;
    [line.layer addAnimation:posiAnimation forKey:nil];
    
     for (NSInteger i = 1; i<_line_points.count; i++) {
        UILabel *line = [self viewWithTag:2048+i];
         double delayInSeconds = 0.1+i*0.1;
         dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds *NSEC_PER_SEC);
         dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
             //执行事件
             [line.layer addAnimation:posiAnimation forKey:nil];
         });
     }
}


- (void)enterBtnAnimation {
    double delayInSeconds = 0.5;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds *NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        
        CGPoint startPoint = _enterBtn.layer.position;
        CGPoint endPoint = CGPointMake(_enterBtn.layer.position.x, _enterBtn.layer.position.y-200);
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
        animation.fromValue         = @(startPoint);
        animation.toValue           = @(endPoint);
        animation.timingFunction    = [CAMediaTimingFunction functionWithControlPoints:0.20 :0.03 :0.13 :1.00];
        animation.duration          = 1.0f;
        //以下两行同时设置才能保持移动后的位置状态不变
        animation.fillMode=kCAFillModeForwards;
        animation.removedOnCompletion = NO;
        [_enterBtn.layer addAnimation:animation forKey:@"btnPosition"];
        _enterBtn.layer.position = endPoint;
        
    });
   
}

- (void)startScale {
    //执行事件
    [self.tipImageView.layer removeAnimationForKey:@"indicatorViewScale"];
    [self.tipLabel.layer removeAnimationForKey:@"indicatorViewScale"];
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.autoreverses = YES;
    scaleAnimation.fromValue = @1.0;
    scaleAnimation.toValue = @1.2;
    scaleAnimation.duration = 1.0;
    scaleAnimation.repeatCount = MAXFLOAT;
    [self.tipImageView.layer addAnimation:scaleAnimation forKey:@"indicatorViewScale"];
    [self.tipLabel.layer addAnimation:scaleAnimation forKey:@"indicatorViewScale"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (flag) {
    
        if ([anim isKindOfClass:[CAAnimationGroup class]]) {//印章第一次复原后
            CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
            scaleAnimation.duration = 0.1;
            scaleAnimation.repeatCount = 1;
            scaleAnimation.fromValue = @1.1;
            scaleAnimation.toValue = @1;
            [self.markImageView.layer addAnimation:scaleAnimation forKey:nil];
            
        }
        else if ([self.lastCircle.layer animationForKey:@"scaleAnimation2"] == anim) {
            
            CABasicAnimation *scaleAnimation2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
            scaleAnimation2.delegate = self;
            scaleAnimation2.duration = 0.3;
            scaleAnimation2.repeatCount = 1;
            scaleAnimation2.fromValue = @0;
            scaleAnimation2.toValue = @1;
            //以下两行同时设置才能保持移动后的位置状态不变
//            scaleAnimation2.fillMode=kCAFillModeForwards;
//            scaleAnimation2.removedOnCompletion = NO;
            [self.tipImageView.layer addAnimation:scaleAnimation2 forKey:@"scaleAnimation3"];
            [self.tipLabel.layer addAnimation:scaleAnimation2 forKey:@"scaleAnimation3"];
            self.tipImageView.layer.transform = CATransform3DScale(CATransform3DIdentity, 1, 1, 1);
            self.tipLabel.layer.transform = CATransform3DScale(CATransform3DIdentity, 1, 1, 1);
        }
        else if ([self.tipImageView.layer animationForKey:@"scaleAnimation3"]) {
            double delayInSeconds = 0.5;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                [self startScale];
            });
            
        }
        else { //曲线动画结束后
           
            
        }
      
    }
    
}

@end
